package com.parseword2;

import java.util.List;

import com.parseword1.WordTest;

public class Word2Main {
	// 分词测试
	public static void main(String[] args) throws Exception {
		Word word = new Word();
		// 获得词库
		StringBuilder ngs = new StringBuilder();
		ngs.append(WordTest.get("/Users/zhangxingyu/project/敏感词库/百度敏感词/百度过滤词.txt")).append("\n");
		ngs.append(WordTest.get("/Users/zhangxingyu/project/敏感词库/百度敏感词/百度敏感词.txt"));
		String[] texts = ngs.toString().split("\n");
		for (String text : texts) {
			text = text.replace("\r", "");
			if(!text.trim().isEmpty()){
				word.set(text);
			}
		}
		System.out.println("词库数量："+texts.length);
		String text = "颠覆hfsdfdsfsbbdk江泽民sjfkdsjfhjkdsfhjsd老哈哈yy通讯录傻b，胡锦涛-hellofuck";
		
		
		ParseWord parseWord = new SimpleParseWord();
		List<ParseWordResult> parseWordResults = word.getAll(text, parseWord);
		System.out.println(parseWordResults);
		System.out.println(format(parseWordResults));
		System.out.println(format(word, parseWord, text));
		int size = 100000;
		long time = System.currentTimeMillis();
		for (int i = 0; i < size; i++) {
			format(word, parseWord, text);
		}
		
		time = System.currentTimeMillis() - time;
		System.out.println("执行次数="+size+",总耗时="+time+"ms,平均"+((double)time/size)+"ms/次");
	}
	
	public static String format(Word word,ParseWord parseWord,String text){
		StringBuilder texts = new StringBuilder(1024);
		while(text!=null){
			
			ParseWordResult parseWordResult = word.get(text, parseWord);
			if(parseWordResult!=null){
				String frontText = parseWordResult.getFrontText();	// 前面的文本
				String wordText = parseWordResult.getWordText();	// 截取词汇
				String rearText = parseWordResult.getRearText();	// 后面的文本
				
				if(frontText!=null){
					texts.append(frontText);
				}
				if(wordText!=null){
					texts.append("*");
				}
				text = rearText;
			}
		}
		return texts.toString();
	}
	public static String format(List<ParseWordResult> parseWordResults){
		int length = parseWordResults.size();
		StringBuilder text = new StringBuilder(1024);
		for (int i = 0; i < length; i++) {
			ParseWordResult parseWordResult = parseWordResults.get(i);
			text.append(parseWordResult.getFrontText()).append("*");
			if(i == length-1){
				if(parseWordResult.getRearText()!=null){
					text.append(parseWordResult.getRearText());
				}
			}
		}
		return text.toString();
	}
}
